<template>
  <div>
    <el-divider> 好友列表 </el-divider>

    <friend-list
      :friend-list="botFriendList"
      @deleteFriended="botGetFriendList"
    />

    <el-divider> 申请加机器人列表 </el-divider>

    <el-button type="primary" plain @click="botCountMessage">刷新状态</el-button>
    <apply-for-adding-friend :meg-list="ApplyForList" @agreeFriend="agreeFriend" />

  </div>
</template>

<script>

import { getFriendList, countMessage } from '@/api/bot'

import FriendList from './ChildCpm/FriendList'
import ApplyForAddingFriend from './ChildCpm/ApplyForAddingFriend'

export default {
  components: {
    FriendList,
    ApplyForAddingFriend
  },
  props: {

  },
  data() {
    return {
      botFriendList: [],
      ApplyForList: []
    }
  },
  created() {
    setTimeout(() => {
      this.botGetFriendList()
      this.botCountMessage()
    }, 100)
  },
  methods: {
    botGetFriendList() {
      getFriendList().then(res => {
        this.botFriendList = res
      }).catch(err => {
        console.log(err)
      })
    },
    botCountMessage() {
      countMessage().then(res => {
        this.ApplyForList = res.filter(item => {
          if (item.type !== 'NewFriendRequestEvent') return false
          for (const i of this.botFriendList) if (i.id === item.fromId) return false
          return true
        })
        if (this.ApplyForList.length === 0) this.$message({ type: 'warning', message: '暂无好友添加' })
      }).catch((err) => {
        console.log(err)
      })
    },
    agreeFriend() {
      setTimeout(() => {
        this.botGetFriendList()
        setTimeout(() => {
          this.botCountMessage()
        }, 100)
      }, 100)
    }
  }
}
</script>

<style scoped>

</style>
